import 'package:flutter/material.dart';

class CateGoryPage extends StatefulWidget {
  @override
  _CateGoryPageState createState() => _CateGoryPageState();
}

class _CateGoryPageState extends State<CateGoryPage>
    with TickerProviderStateMixin {
  List<TimerChooseDataModel> timerDataSource = [];
  List<DayChooseMode> daysDataSource = [];
  @override
  void initState() {
    int startHour = 8;
    int endHour = 50;

    for (var i = startHour; i <= endHour; i++) {
      timerDataSource.add(TimerChooseDataModel(
        timer: "$i:00",
        canChoose: i >= 17 ? false : true,
      ));
      timerDataSource.add(TimerChooseDataModel(
        timer: "$i:00",
        canChoose: i >= 17 ? false : true,
      ));
    }

    for (var i = 0; i < 9; i++) {
      daysDataSource.add(DayChooseMode(weak: "星期$i", day: "8月$i号"));
    }

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    BorderSide borderSide =
        BorderSide(color: Colors.black, width: 1, style: BorderStyle.solid);
    List<Widget> widgets = this.timerDataSource.map((TimerChooseDataModel e) {
      return GestureDetector(
        onTap: () {
          if (e.canChoose) {
            print(e.timer);
            setState(() {
              e.canChoose = false;
            });
          } else {
            print("当前时间不可选择1！");
          }
        },
        child: Container(
          child: Center(child: Text('${e.timer}')),
          decoration: BoxDecoration(
            color: e.canChoose ? Colors.white : Colors.blueGrey[50],
            border: Border(
              bottom: borderSide,
              right: borderSide,
            ),
          ),
        ),
      );
    }).toList();

    List<Widget> daysWidgets = this.daysDataSource.map((DayChooseMode e) {
      return Container(
        width: MediaQuery.of(context).size.width * 0.25,
        child: Padding(
          padding: const EdgeInsets.all(10.0),
          child: Column(
            children: [
              Text(e.weak),
              Text(e.day),
            ],
          ),
        ),
      );
    }).toList();

    return Scaffold(
      appBar: AppBar(title: Text("约课")),
      body: Column(
        children: [
          Container(
            //height: 44,
            child: SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: Row(children: daysWidgets),
            ),
          ),
          Expanded(
            child: GridView(
              primary: false,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 4,
                childAspectRatio: 2.0,
              ),
              children: widgets,
            ),
          ),
        ],
      ),
    );
  }
}

class TimerChooseDataModel {
  String timer;
  bool canChoose;
  bool userChoose;

  TimerChooseDataModel({this.timer, this.canChoose});
}

class DayChooseMode {
  final String weak;
  final String day;

  DayChooseMode({this.weak, this.day});
}
